<?php

namespace app\sys\controller;

use think\Controller;

class Base extends Controller
{
    /**
     * @throws \think\Exception\DbException
     */
    public function initialize() {
        parent::initialize();
        //判断是否登录
        $user = session('loginUser');
        if ($user == null) {
            $u = url('/sys/login/login');
            exit("<script type='text/javascript'>top.location.replace('$u');</script>");
        }
        //请求的URL
        $url = $this->request->baseUrl();
        //判断是否以.html结尾
        if (strrpos($url, '.html') > 0) {
            $url = substr($url, 0, strrpos($url, '.html'));
        }
        if($url !== '/sys/index/desktop'){
            $ps_model = model('common/PermissionModel');
            $permissions = $ps_model->get_login_user_permissions($user->name);
            if (!in_array($url, $permissions)) {
                if ($this->request->isAjax()) {
                    $array = array(
                        'status' => false,
                        'msg' => '没有权限访问该模块:' . $url,
                    );
                } else {
                    exit("<script type='text/javascript'>alert('没有权限访问该模块123');window.history.back();</script>");
                }
            }
        }
    }
}
